#!/usr/bin/env zsh
# Helper to change the numerical prefixes across the repo, say if you want to
# create room between 023 and 024, and so on.

setopt extendedglob

if [[ $# -eq 0 && `git diff HEAD |wc -l` -gt 0 ]]
then
  echo "Uncommitted changes"
  exit
fi

if [[ $# -gt 0 ]] # dry run
then
  git() {
    echo $*
  }
fi

#

index=0
ls [0-9](^*test.)cc [0-9]*.wart |sort -n |sed 's/\..*//' |
  while read file
  do
    while [[ $file != `printf "%03d" $index`* ]]
    do
      echo
      index=$(($index+1))
    done
    echo $file
    index=$(($index+1))
  done > .layout

vim -c "set nu" .layout

#

root() {
  echo $1 |sed 's/^[0-9]*//' |sed 's/\..*//'
}

index=0
cat .layout |
  while read file
  do
    if [ ! -z $file ]
    then
      newfile=`printf "%03d" $index``root $file`
      if [[ $newfile != $file ]]
      then
        [[ -f $file.cc ]] && git mv $file.cc $newfile.cc
        [[ -f $file.test.cc ]] && git mv $file.test.cc $newfile.test.cc
        [[ -f $file.wart ]] && git mv $file.wart $newfile.wart
        [[ -f $file.test ]] && git mv $file.test $newfile.test
      fi
    fi
    index=$(($index+1))
  done

#

rm .layout
